<?php 
/**
 * 
 * Modelo de presupuesto
 * @author Natanael
 * 
 */

class mpresupuesto extends Model{
	
	private $mTableName = "GW_Presupuesto";
	
	public function __construct()
	{
		parent::model();
	}
	
   public function crearRegistro($data)
	{
		if ($this->db->insert($this->mTableName,$data)) {
			return true;
		}else{
			return false;
		}
	}
	
	public function editRegistro($data,$id)
	{
	if ($this->db->update($this->mTableName,$data,array('id'=>$id))) {
			return true;
		}else{
			return false;
		}
	}
	
    public function loadRegistroById($id)
	{	
		   $qresult = $this->db->get_where($this->mTableName, array('id' => $id));
			if ($qresult->num_rows() == 0)
				return false;
	
			return new PresupuestoS($qresult->row());
	}
	
	public function loadGrafico()
	{   
		$this->db->limit(10);
		$this->db->order_by("anos", "desc");
		$this->db->order_by("wk", "desc");

		$result = $this->db->get_where($this->mTableName);
		return $this->GetArrayFromQueryResult($result);
	}
	
	public function loadPresupuesto($start = false, $offset = false, $sortname = false, $sortorder = false)
	{
		  if($offset != false)
				$this->db->limit($offset, $start);

			if($sortname != false && $sortorder != false) {
				$this->db->order_by($sortname, $sortorder);
			}
	     
			
			$result = $this->db->get_where($this->mTableName);
	
			return $this->GetArrayFromQueryResult($result);
	}
	
   public function GetArrayFromQueryResult($result) 
	{
			if ($result->num_rows() == 0)
				return false;
	
			$estatus_arr = array();
	
			foreach ($result->result() as $record) {
				$estatus_arr[] = new PresupuestoS($record);
			}
	
			return $estatus_arr;
    }
    
  public function delete($id)
    {
        if ($this->db->delete($this->mTableName,array('id'=>$id))) {
        	return true;
        }else{
        	return false;
        }	
    }
}

class PresupuestoS{
	
	private $id = 0;
	private $anos = "";
	private $wk = 0;
	private $presupuesto = 0;
	
	public function __construct($ojt)
	{
		$this->setId($ojt->id);
		$this->setAnos($ojt->anos);
		$this->setWk($ojt->wk);
		$this->setPresupuesto($ojt->presupuesto);
	}
	
	public function setId($ojt)
	{
		$this->id = $ojt;
	}
	
	public function getId()
	{
	   return $this->id;	
	}
	
	public function setAnos($ojt)
	{
		$this->anos = $ojt;
	}
	
	public function getAnos()
	{
		return $this->anos;
	}
	
	public function setWk($ojt)
	{
		$this->wk = $ojt;
	}
	
	public function getWk()
	{
		return $this->wk;
	}
	
	public function setPresupuesto($ojt)
	{
		$this->presupuesto = $ojt;
	}
	
	public function getPresupuesto()
	{
		return $this->presupuesto;
	}
}
?>